<?php

abstract class Base_Path extends Base_Base implements ArrayAccess
{
	public $_path;
	function __construct(&$path = 0)
	{
        //Parses the path into an array of segments
        $this->_path = $this->_parsePath($this->_customName(),$path);
        //If the first segment of the path is empty or is identical as the name of the class, then it should be the index
        if (empty($this->_path[0]) || ($this->_path[0] == $this->_customName() && count($this->_path) <= 1)) {
            $this->_path[0] = 'index';
        }
	}
	function offsetExists($i)
	{
		if (isset($this->_path[$i])) return TRUE;
	}
	function offsetGet($i)
	{
		return $this->_path[$i];
	}
	function offsetSet($i,$v)
	{
		return $this->_path[$i] = $v;
	}
	function offsetUnset($i)
	{
		unset($this->_path[$i]);
		$this->_path = array_values($this->_path);    
	}
    final static public function _choosePath(&$path)
    {
        //If there's no path variable and there's a PATH_INFO global, then that global should be the path
        if (empty($path) && !empty($_SERVER['PATH_INFO'])) $path = $_SERVER['PATH_INFO'];
        //If the path still empty, returns an index path else return the path
        return empty($path) ? '/index' : $path;
    }
    final static public function _parsePath($name, &$path = 0)
    {
        if (is_array($path)) {
            $path = array_values($path);   
        } else {
            //This one splits the array by slashes ignoring the first segment if that segment has the value of the name variable
            $path = preg_split('#(^/'.$name.')?/#i', self::_choosePath($path), -1, PREG_SPLIT_NO_EMPTY);
        }
        return $path;
    }
    final static public function _chooseAppName($between, $path = 0)
    {
    	if (empty($between)) throw new Base_Exception('You need to specify App names to choose from');
        if (!is_array($path)) {
            $path = Base_Path::_choosePath($path);
            //Get the app name from a regular expression using between variable
            $appName = preg_replace('#^/('.$between.')((/[^/]+)*/?)$#i','$1',$path);
            //The replace didn't work
            if ($appName == $path) {
                //Get the first between segment, put matches in appName variable
                preg_match('#^[^\|]+#', $between, $appName);
                //appName is the first match from between variable
                $appName = $appName[0];
            }         
        } else {
            //First segment of the path matches one of the segments of between
            if (in_array($path[0], explode('|', $betweeen))) {
                //Appname is the first segment of the path
                $appName = $path[0];
            }
        }
        //Adds the sulfix to the appName
        return ucfirst($appName).'_App';
    }
}

?>